﻿using LJ.EventBus.HornedSerpent.Transport.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LJ.EventBus.HornedSerpent.Internal;

public class EventBusMessageSerializer : IEventBusMessageSerializer
{
    private readonly static JsonSerializerOptions _jsonSerializerOptions = new(JsonSerializerDefaults.Web);



    public T Deserialize<T>(string json)
    {
        return JsonSerializer.Deserialize<T>(json, _jsonSerializerOptions)!;
    }

    public object Deserialize(string json, Type type)
    {
        return JsonSerializer.Deserialize(json, type, _jsonSerializerOptions)!;
    }

    public string Serialize(object value)
    {
        ArgumentNullException.ThrowIfNull(value, nameof(value));
        return JsonSerializer.Serialize(value, _jsonSerializerOptions);
    }

    public byte[] ToBytes(string value)
    {
        return Encoding.UTF8.GetBytes(value);
    }


    public string ToJson(byte[] bytes)
    {
        return Encoding.UTF8.GetString(bytes);
    }
}
